package com.slkj.shilixiaoyuanapp.ui.main.message;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.contacts.PersonBean;
import com.slkj.shilixiaoyuanapp.util.KeyWordUtil;
import com.slkj.shilixiaoyuanapp.util.PinyingUtils;


import java.util.ArrayList;
import java.util.List;

public class SearchAdaper extends BaseQuickAdapter<PersonBean,BaseViewHolder> {
    private Context context;
    private ArrayList<PersonBean> personBeanList;
    /**
     * 搜索文本
     */
    private List<String> searchTxtList = new ArrayList<>();
    public void setSearchTxt(List<String> list) {
        searchTxtList.clear();
        if (list != null)
            searchTxtList.addAll(list);
    }

    public SearchAdaper(Context context, @Nullable ArrayList<PersonBean> personBeanArrayList) {
        super(R.layout.item_message_contacts_person, personBeanArrayList);
        this.context = context;
        this.personBeanList = personBeanArrayList;
    }
    @Override
    protected void convert(BaseViewHolder helper, PersonBean item) {
        Glide.with(context).load(item.getHead())
                .error(R.drawable.ic_man)
                .placeholder(R.drawable.ic_man)
                .into((ImageView) helper.getView(R.id.img_head));
        TextView tvName = helper.getView(R.id.tv_name);
        TextView tvPhone = helper.getView(R.id.tv_phone);
        ImageView call = helper.getView(R.id.img_call);
        if (item.getName() != null && item.getName().trim().length() > 0) {
            tvName.setText(KeyWordUtil.matcherSearchTitle(ContextCompat.getColor(context, R.color.color_red),
                    item.getName(), filterSearchList(item.getName(), searchTxtList), false));
        } else {
            tvName.setText("-");
        }
        if (item.getPhone() != null && item.getPhone().trim().length() > 0) {
            tvPhone.setText(KeyWordUtil.matcherSearchTitle(ContextCompat.getColor(context, R.color.color_red),
                    item.getPhone(), filterSearchList(item.getPhone(), searchTxtList), false));
        } else {
            tvPhone.setText("-");
        }
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                SimpleHintDialog simpleHintDialog = new SimpleHintDialog(context);
//                simpleHintDialog.setTitleString("确认拨打改电话？");
//                simpleHintDialog.setContentString(item.getPhone());
//                simpleHintDialog.setOnEnterClickListener(new OnEnterClickListener() {
//                    @Override
//                    public void onEnter() {
//                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + item.getPhone()));
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        context.startActivity(intent);
//                    }
//                });
//                simpleHintDialog.show();
            }
        });
    }

    /**
     * 搜索关键字从英文分析到中文哪些是关键字
     */
    private List<String> filterSearchList(String source, List<String> list) {
        // 结果集列表
        List<String> resList = new ArrayList<>();
        if (source == null || source.trim().length() == 0)
            return resList;
        boolean keyIsEnglish = true;// 搜索关键字是否是英文
        for (int i = 0; i < list.size(); i++) {
            if (PinyingUtils.isChinese(list.get(i))) {// 搜索关键字里面只要有一个为中文的话就不进行拆分比对
                keyIsEnglish = false;
                break;
            }
        }
        if (PinyingUtils.isChinese(source) && keyIsEnglish) {// 只有中文才过滤
            char arr[] = source.toCharArray();
            for (int i = 0; i < arr.length; i++) {
                // 得到源字符串的全拼和简拼
                String first = PinyingUtils.cn2FirstSpell(arr[i] + "");
                String spell = PinyingUtils.cn2Spell(arr[i] + "");
                if (first.trim().length() > 0 && spell.trim().length() > 0) {//排除掉字符串转换为拼写时为空的情况
                    for (int j = 0; j < list.size(); j++) {
                        if (list.get(j).contains(first) || list.get(j).contains(spell)) {// 全拼和简拼满足一个即可标识为关键字
                            resList.add(arr[i] + "");
                            break;
                        }
                    }
                }
            }
            return resList;
        } else {
            return list;
        }
    }
}
